﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// UserCo 的摘要说明
/// </summary>
public class UserCoDAO
{
    SqlHelp help = new SqlHelp();
    private string sql = string.Empty;
    public UserCoDAO()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    public DataTable[] GetUserCoInti()
    {
        return help.RunProcGetTables("GetUserCoInti", null);
    }

    public int AddUserCo(UserCoModel model)
    {
        sql = " insert into B_UserCo(UserCode, CoCode, UserCoDesc, DeleteFlag, AddAuthor, AddDate, UpdateAuthor, UpdateDate) "
            + " values(@UserCode, @CoCode, @UserCoDesc, @DeleteFlag, @AddAuthor, @AddDate, @UpdateAuthor, @UpdateDate)";
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@CoCode",model.CoCode),
            new SqlParameter("@UserCoDesc",model.UserCoDesc),
            new SqlParameter("@DeleteFlag","0"),
            new SqlParameter("@AddAuthor",model.UpdateAuthor),
            new SqlParameter("@AddDate",model.UpdateDate),
            new SqlParameter("@UpdateAuthor",model.UpdateAuthor),
            new SqlParameter("@UpdateDate",model.UpdateDate)
        };
        return help.RunSqlInt(sql, paras);
    }

    public int EditUserCo(UserCoModel model)
    {
        sql = " update B_UserCo set CoCode=@CoCode, UserCoDesc=@UserCoDesc,  UpdateAuthor=@UpdateAuthor, UpdateDate=@UpdateDate where UserCode=@UserCode ";
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@CoCode",model.CoCode),
            new SqlParameter("@UserCoDesc",model.UserCoDesc),
            new SqlParameter("@UpdateAuthor",model.UpdateAuthor),
            new SqlParameter("@UpdateDate",model.UpdateDate)
        };
        return help.RunSqlInt(sql, paras);
    }

    public int DeleteUserCo(UserCoModel model)
    {
        sql = " delete from B_UserCo where UserCode=@UserCode and CoCode=@CoCode ";
        SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@UserCode",model.UserCode),
            new SqlParameter("@CoCode",model.CoCode)
        };
        return help.RunSqlInt(sql, paras);
    }

    public DataTable GetUserCoQuery(string str)
    {
        sql = " select	UC.UserCode, U.UserFullName+'---'+U.UserEngName UserName, UC.CoCode,C.CoName, UC.DepCode, UC.UserCoDesc, UC.DeleteFlag, "
            + "UC.AddAuthor, UC.AddDate, UC.UpdateAuthor, UC.UpdateDate from	B_UserCo UC join B_User U on U.UserCode=UC.UserCode join B_Company C on C.CoCode=UC.CoCode where UC.DeleteFlag<>1 " + str;

        return help.GetDataTable(sql);
    }
}
